package com.atguigu.gmall.pms.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jca.cci.core.support.CciDaoSupport;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.CategoryMapper;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import com.atguigu.gmall.pms.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;
    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<CategoryEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<CategoryEntity> queryThread() {
        //获取所有的分类
        List<CategoryEntity> categoryEntities = this.baseMapper.selectList(null);
        List<CategoryEntity> collect = categoryEntities.stream().filter(Item -> Item.getParentId()==0).map(parent -> {
            parent.setSubs(getChild(categoryEntities, parent));
            return parent;
        }).collect(Collectors.toList());
        return collect;
    }

    private List<CategoryEntity> getChild(List<CategoryEntity> categoryEntities, CategoryEntity parent) {
        //获取子分类
        List<CategoryEntity> collect = categoryEntities.stream().filter(item -> item.getParentId().equals(parent.getId())).map(child -> {
            //递归调用
            child.setSubs(getChild(categoryEntities, child));
            return child;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<CategoryEntity> queryCategoriesByPid(Long pid) {

        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        if (pid != -1){
            wrapper.eq("parent_id", pid);
        }
        return this.list(wrapper);
    }

    @Override
    public List<CategoryEntity> queryLvl12CategoriesByPid(Long pid) {

        return categoryMapper.queryLvl12CategoriesByPid(pid);
    }

    @Override
    public List<CategoryEntity> queryCategory12ByCid(Long cid) {
        //查找三级分类
        CategoryEntity categoryEntity3 = this.baseMapper.selectById(cid);
        if (categoryEntity3==null){
            return null;
        }
        //查询3级分类的父id
        CategoryEntity categoryEntity2 = this.baseMapper.selectOne(new QueryWrapper<CategoryEntity>().eq("id", categoryEntity3.getParentId()));

        //查找一级分类
        CategoryEntity categoryEntity1 = this.baseMapper.selectOne(new QueryWrapper<CategoryEntity>().eq("id", categoryEntity2.getParentId()));
        List<CategoryEntity> categoryEntities = new ArrayList<>();
        categoryEntities.add(categoryEntity1);
        categoryEntities.add(categoryEntity2);
        categoryEntities.add(categoryEntity3);

        return categoryEntities;
    }


}